package com.kr.aia.powerrp.db;

import java.util.ArrayList;
import java.util.List;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.widget.Toast;

public class RPGroupDBUtil {

	// Database fields
	private SQLiteDatabase database;
	private MySQLiteHelper dbHelper;
	private Context context;
	
	/*
	TABLE_RP_GROUP				= "RP_GROUP";
	public static final String COLUMN_RP_GROUP_ID			= "GROUP_ID";
	public static final String COLUMN_RP_GROUP_FILE_NAME	= "FILE_NAME";
	public static final String COLUMN_RP_GROUP_FILE_ORDER	
	*/
	
	
	private String[] allColumns = { MySQLiteHelper.COLUMN_RP_GROUP_ID, 
									MySQLiteHelper.COLUMN_RP_GROUP_FILE_NAME, 
									MySQLiteHelper.COLUMN_RP_GROUP_FILE_ORDER
								   };
	
	public RPGroupDBUtil(Context context) {
		dbHelper = new MySQLiteHelper(context);
		this.context = context;
	}

	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}

	public void close() {
		dbHelper.close();
	}
	
	
	public RPGroupInfoVO insertRPGroupInfo(RPGroupInfoVO rpGroupInfo, int groupId) {
		ContentValues values = new ContentValues();
		values.put(MySQLiteHelper.COLUMN_RP_GROUP_ID, rpGroupInfo.getGroupId());
		values.put(MySQLiteHelper.COLUMN_RP_GROUP_FILE_NAME, rpGroupInfo.getFileName());
		values.put(MySQLiteHelper.COLUMN_RP_GROUP_FILE_ORDER, rpGroupInfo.getFileOrder());
		
		long insertId = database.insert(MySQLiteHelper.TABLE_RP_GROUP, null, values);
		
		// To show how to query
		Cursor cursor = database.query(MySQLiteHelper.TABLE_RP_GROUP,
				allColumns, MySQLiteHelper.COLUMN_RP_GROUP_ID + " = " + groupId, null, null, null, null);
		cursor.moveToFirst();
		
		if(insertId > -1){
			//Toast.makeText(context, "DB에 저장 되었습니다.", Toast.LENGTH_LONG).show();
			Log.i("PRP", "Successfully inserted data to the table");
		}else{
			//Toast.makeText(context, "저장 실패!!", Toast.LENGTH_LONG).show();
			Log.i("PRP", "Failed insert data to the table");
		}
		
		return cursorToRPGroupInfo(cursor);
	}
	
	
	public void deleteRPGroup(int groupId){
		database.delete(MySQLiteHelper.TABLE_RP_GROUP, MySQLiteHelper.COLUMN_RP_GROUP_ID + " = " + groupId, null);
	}
	

	public ArrayList<RPGroupInfoVO> getAllData(int groupId) {
		ArrayList<RPGroupInfoVO> rpGroupList = new ArrayList<RPGroupInfoVO>();
		
		RPGroupInfoVO rpGroupInfo = null;
		Cursor cursor = database.query(MySQLiteHelper.TABLE_RP_GROUP, allColumns, MySQLiteHelper.COLUMN_RP_GROUP_ID + " = " + groupId, null, null, null, MySQLiteHelper.COLUMN_RP_GROUP_FILE_ORDER);
		if(cursor.getCount() > 0){
			while(cursor.moveToNext()){
				rpGroupInfo = cursorToRPGroupInfo(cursor);
				rpGroupList.add(rpGroupInfo);
			}
		}
		cursor.close();
		
		return rpGroupList;
	}

	private RPGroupInfoVO cursorToRPGroupInfo(Cursor cursor) {
		RPGroupInfoVO rpGroupInfo = new RPGroupInfoVO();
		
		rpGroupInfo.setGroupId(cursor.getInt(0));
		rpGroupInfo.setFileName(cursor.getString(1));
		rpGroupInfo.setFileOrder(cursor.getInt(2));
		
		return rpGroupInfo;
	}
}